'use client'

import { I18nextProvider } from 'react-i18next'
import i18n from '@/lib/i18n/index'
import { useEffect } from 'react'
import Cookies from 'js-cookie'

export function I18nProvider({ children }: { children: React.ReactNode }) {
  // 初始化时从 cookie 读取语言设置
  useEffect(() => {
    const savedLocale = Cookies.get('NEXT_LOCALE')
    if (savedLocale && i18n.language !== savedLocale) {
      i18n.changeLanguage(savedLocale)
    }
  }, [])

  return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>
}